#ifndef ANDROID_CONTROLLER_HH
#define ANDROID_CONTROLLER_HH

#include "ros/ros.h"
#include "std_msgs/String.h"
#include "android_service/android_service.h"
#include "android_service/android_message.h"

#include <sstream>
#include <iostream>

using namespace std;

const int MSG_ID_BASE = 1000;

class Android_Controller
{
public:
    Android_Controller();
    Android_Controller(string user_name);
    ~Android_Controller();
    void init(string uname);

    void android_voice_callback(const android_service::android& msg);
    void android_map_callback(const android_service::android& msg);
    void android_publish(const android_service::android& msg);

    void usage();
    string itos(int i);

private:
	ros::NodeHandle nh_;
    ros::Publisher  android_pub;
    ros::Subscriber android_voice;
    ros::Subscriber android_map;
    unsigned long msg_id;
    string user_name;
};

#endif

Android_Controller::Android_Controller() 
{
    init("zeddy");
}

Android_Controller::Android_Controller(string _user_name) 
{
    init(_user_name);
}
Android_Controller::~Android_Controller() 
{

}   
void Android_Controller::init(string uname)
{ 
    android_pub = nh_.advertise<android_service::android>("android_request", 1);
    android_voice = nh_.subscribe("android_voice",1, 
              &Android_Controller::android_voice_callback, this);
    android_map = nh_.subscribe("android_map",1, 
              &Android_Controller::android_map_callback, this);
    msg_id = MSG_ID_BASE;
    user_name.assign(uname);
}

void Android_Controller::android_voice_callback(const android_service::android& msg)
{
    cout <<"info: android_voice_callback() : "<<endl; 
    for(int i = 0; i < msg.record.size(); i ++) {
        cout <<"    [ "<<msg.record[i]<<endl;
    } 
}
void Android_Controller::android_map_callback(const android_service::android& msg)
{
    cout <<"info: android_callback() : "<<endl; 
    for(int i = 0; i < msg.record.size(); i ++) {
        cout <<"    [ "<<msg.record[i]<<endl;
    } 
}
void Android_Controller::android_publish(const android_service::android& msg)
{
    msg_id++;
    android_pub.publish(msg);
}
void Android_Controller::usage() {
    string usage = "usag()::\n   VoiceClient  -h\n";
    usage += "   VoiceClient <msgType>:<clientid>:<field1>:<field2>...<fieldn>\n";
    usage += "      where msgTye {\n";
    usage += "\t[3] ANDROID_BROADCAST,  // one way voice message - \n";
    usage += "\t[4] ANDROID_QUESTION,   // Question with replay - \n";
    usage += "\t[5] ANDROID_GRID_LOCATION   // Ask humam for a desired map location - \n";
    usage += "\t[6] ANDROID_BIOMETRICS // ask Android to generate facial biometric signature - \n";
    usage += "      }\n\n";
    usage += "      e.g. 3:zeddy:Ask humam for a desired map location\n";
    usage += "      e.g. 4:zeddy:Could you help me please?\n";
    usage += "      e.g. 5:zeddy:Where is west point? Touch it on my touch Tablet.\n";
    usage += "      e.g. 6:zeddy:/sdcard/data/zeddy.png\n";
    usage += "----------------------------------------------------------";
    cout <<usage<<endl;
}
string Android_Controller::itos(int i) {
    stringstream ss;
    ss << i;
    return ss.str();
}
 

int main(int argc, char **argv)
{
    stringstream strbuf;
    vector<string> bioMsg; 
    int message_counter = 1000;
    int MAX_MSG_SIZE = 512;
	ros::init(argc, argv, "AndroidController");
    Android_Controller rsc;
	ros::Rate loop_rate(1);
	char buff[512+1];
    Message android_msg;
    string myBuffer;
    rsc.usage();

	while (ros::ok())
	{
        android_msg.reset();
       
         
        android_service::android msg;
        cout <<"Enter Activity : ";
        memset(buff,0,MAX_MSG_SIZE);
        if(cin.getline(buff,MAX_MSG_SIZE).eof() || (strlen(buff) <= 1)) {
            continue;
        }
        myBuffer.assign(buff);

        if(strncmp(myBuffer.c_str(),"-h",2) == 0) {
            rsc.usage();
            continue;
        }
            
        if(android_msg.read(myBuffer, false) != 0) {
                cout <<"Error: [ "<<buff<<" ] AndroidMsg malformed..."<<endl;
                continue;
        }
        if(android_msg.message_type() == ANDROID_UNDEFINED) {
            cout <<"Undefined messagetype "<<endl;
            continue;
        }
        android_msg.insert_entry(1,rsc.itos(message_counter++));
           
        msg.msg_type = android_msg.message_type();
        for(int i = 0; i < android_msg.size(); i ++) {
            msg.record.push_back(android_msg.entry(i));
        } 
        rsc.android_publish(msg);
		ros::spinOnce();
		loop_rate.sleep();
        strbuf.str("");
	}
	return 0;
}
